package ttot.transitions.plugin

import java.nio.file.{Paths, Path}
import org.codehaus.plexus.util.DirectoryScanner

object ClassFinder {

  def find(path: Path): Set[Path] = classesIn(findFiles(path)).map(p => p.toAbsolutePath)

  def findFiles(path: Path): Set[Path] = {
    val concrete = path.toFile
    if (!concrete.exists() || !concrete.isDirectory) Set[Path]()
    val scanner = new DirectoryScanner
    scanner.setBasedir(concrete)
    scanner.setFollowSymlinks(false)
    scanner.setCaseSensitive(true)
    scanner.scan()
    scanner.getIncludedFiles.map(x => path.resolve(Paths.get(x)).toAbsolutePath).toSet
  }

  def classesIn(files: Set[Path]): Set[Path] = files.filter(p => p.getFileName.toString.endsWith(".class"))

}
